<script setup lang="ts">
import { computed, ref, watch } from 'vue';

interface Pet {
  id: string;
  name: string;
  ownerName: string;
  ownerPhone: string;
  type: string;
  breed?: string;
  gender?: string;
  age?: string;
  neutered?: string;
  doctor: string;
  phone: string;
}

interface Props {
  pets?: Pet[];
  selectedPet?: null | Pet;
  title?: string;
  emptyText?: string;
  activeTab?: string;
}

interface Emits {
  (e: 'update:selectedPet',pet: null | Pet): void;
  (e: 'update:activeTab',tab: string): void;
  (e: 'search',keyword: string): void;
  (e: 'tabChange',tab: string): void
}

const props = withDefaults(defineProps<Props>(), {
  pets: () => [],
  selectedPet: null,
  title: '选择宠物',
  emptyText: '暂无宠物数据',
  activeTab: 'today',
});

const emit = defineEmits<Emits>();

// 响应式数据
const searchKeyword = ref('');
const internalActiveTab = ref(props.activeTab);

// 计算属性
const filteredPets = computed(() => {
  if (!searchKeyword.value) {
    return props.pets;
  }
  return props.pets.filter(
    (pet) =>
      pet.name.includes(searchKeyword.value) ||
      pet.ownerName.includes(searchKeyword.value) ||
      pet.ownerPhone.includes(searchKeyword.value),
  );
});

// 方法
const getPetIcon = (type: string) => {
  const iconMap: Record<string, string> = {
    狗: '🐕',
    猫: '🐱',
    兔子: '🐰',
    鸟: '🐦',
    鱼: '🐠',
    仓鼠: '🐹',
    其他: '🐾',
  };
  return iconMap[type] || '🐾';
};

const handlePetSelect = (pet: Pet) => {
  emit('update:selectedPet', pet);
};

const handleSearch = () => {
  emit('search', searchKeyword.value);
};

const handleTabChange = (tab: string) => {
  internalActiveTab.value = tab;
  emit('update:activeTab', tab);
  emit('tabChange', tab);
};

// 监听外部activeTab变化
watch(
  () => props.activeTab,
  (newTab) => {
    internalActiveTab.value = newTab;
  },
);

// 监听内部activeTab变化
watch(internalActiveTab, (newTab) => {
  emit('update:activeTab', newTab);
});
</script>

<template>
  <div class="pet-selector">
    <div class="pet-selection">
      <h3>{{ title || '选择宠物' }}</h3>

      <!-- 标签页 -->
      <a-tabs :active-key="internalActiveTab" @change="handleTabChange">
        <a-tab-pane key="today" tab="今日挂号">
          <div class="search-section">
            <a-input-search
              :value="searchKeyword"
              placeholder="顾客/宠物/手机号"
              @search="handleSearch"
              @update:value="searchKeyword = $event"
              style="width: 100%"
            />
          </div>

          <div class="pets-list">
            <div
              v-for="pet in filteredPets"
              :key="pet.id"
              class="pet-item"
              :class="{ active: selectedPet?.id === pet.id }"
              @click="handlePetSelect(pet)"
            >
              <div class="pet-icon">
                {{ getPetIcon(pet.type) }}
              </div>
              <div class="pet-info">
                <div class="pet-name">{{ pet.name }}({{ pet.ownerName }})</div>
                <div class="pet-detail">{{ pet.doctor }} {{ pet.phone }}</div>
              </div>
            </div>

            <!-- 空状态 -->
            <div v-if="filteredPets.length === 0" class="empty-state">
              <div class="empty-icon">🐾</div>
              <p>{{ emptyText || '暂无宠物数据' }}</p>
            </div>
          </div>
        </a-tab-pane>

        <a-tab-pane key="all" tab="全部">
          <div class="search-section">
            <a-input-search
              :value="searchKeyword"
              placeholder="顾客/宠物/手机号"
              @search="handleSearch"
              @update:value="searchKeyword = $event"
              style="width: 100%"
            />
          </div>

          <div class="pets-list">
            <div
              v-for="pet in filteredPets"
              :key="pet.id"
              class="pet-item"
              :class="{ active: selectedPet?.id === pet.id }"
              @click="handlePetSelect(pet)"
            >
              <div class="pet-icon">
                {{ getPetIcon(pet.type) }}
              </div>
              <div class="pet-info">
                <div class="pet-name">{{ pet.name }}({{ pet.ownerName }})</div>
                <div class="pet-detail">{{ pet.doctor }} {{ pet.phone }}</div>
              </div>
            </div>

            <!-- 空状态 -->
            <div v-if="filteredPets.length === 0" class="empty-state">
              <div class="empty-icon">🐾</div>
              <p>{{ emptyText || '暂无宠物数据' }}</p>
            </div>
          </div>
        </a-tab-pane>
      </a-tabs>
    </div>
  </div>
</template>

<style scoped>
.pet-selector {
  width: 100%;
  height: 100%;
}

.pet-selection {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 16px;
}

.pet-selection h3 {
  margin-bottom: 16px;
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.search-section {
  margin-bottom: 16px;
}

.pets-list {
  flex: 1;
  max-height: calc(100vh - 200px);
  overflow-y: auto;
}

.pet-item {
  display: flex;
  align-items: center;
  padding: 12px;
  margin-bottom: 8px;
  cursor: pointer;
  background: white;
  border: 1px solid #e8e8e8;
  border-radius: 4px;
  transition: all 0.3s;
}

.pet-item:hover {
  background: #f6ffed;
  border-color: #1890ff;
}

.pet-item.active {
  background: #e6f7ff;
  border-color: #1890ff;
}

.pet-icon {
  margin-right: 12px;
  font-size: 24px;
}

.pet-info {
  flex: 1;
}

.pet-name {
  margin-bottom: 4px;
  font-size: 14px;
  font-weight: bold;
  color: #333;
}

.pet-detail {
  font-size: 12px;
  color: #666;
}

.empty-state {
  padding: 40px 20px;
  color: #999;
  text-align: center;
}

.empty-icon {
  margin-bottom: 16px;
  font-size: 48px;
}

.empty-state p {
  margin: 0;
  font-size: 14px;
}

:deep(.ant-tabs-content) {
  height: calc(100vh - 200px);
  overflow-y: auto;
}

:deep(.ant-tabs-tabpane) {
  display: flex;
  flex-direction: column;
  height: 100%;
}

:deep(.ant-tabs-tabpane-active) {
  flex: 1;
}
</style>
